;(function($){
	function PaginationForNum(ele, opts, func){
		this.init(ele, opts, func);
	}
	
	PaginationForNum.prototype = {
		constructor: PaginationForNum,
		//链接算法（当处于link模式）,参数n为页码
		getLink	: function(n){
			//这里的算法适用于比如：
			//hrefFormer=http://www.xx.com/news/20131212
			//hrefLatter=.html
			//那么首页（第1页）就是http://www.xx.com/news/20131212.html
			//第2页就是http://www.xx.com/news/20131212_2.html
			//第n页就是http://www.xx.com/news/20131212_n.html
			if(n == 1){
				return this.hrefFormer + this._opts._opts.hrefLatter;
			}
			return this._opts.hrefFormer + '_' + n + this._opts.hrefLatter;
		},
		//页码单击事件处理函数（当处于mode模式）,参数n为页码
		click	: function(n){
			this.getData(n);
			return false;
		},
		//获取href的值（当处于mode模式）,参数n为页码
		getHref	: function(n){
			//默认返回'javascript:;'
			return 'javascript:;';
		},
		//跳转框得到输入焦点时
		focus_gopage : function (){
			var btnGo = $('#'+this._opts.gopageButtonId);
			$('#'+this._opts.gopageTextboxId).attr('hideFocus',true);
			btnGo.show();
			btnGo.css('left','0px');
			$('#'+this._opts.gopageWrapId).css('border-color','#6694E3');
			btnGo.animate({left: '+=44'}, 50,function(){
				//$('#'+this._opts.gopageWrapId).css('width','88px');
			});
		},
		//跳转框失去输入焦点时
		blur_gopage : function(){
			var opts = this._opts;
			setTimeout(function(){
				var btnGo = $('#'+opts.gopageButtonId);
				btnGo.animate({
				    left: '-=44'
				  }, 100, function(){
					  btnGo.css('left','0px');
					  btnGo.hide();
					  $('#'+opts.gopageWrapId).css('border-color','#DFDFDF');
				  });
			},400);
		},
		//跳转输入框按键操作
		keypress_gopage : function(event){
			var event = arguments[0] || window.event;
			var code = event.keyCode || event.charCode;
			//delete key
			if(code == 8) return true;
			//enter key
			if(code == 13){
				this.gopage();
				return false;
			}
			//copy and paste
			if(event.ctrlKey && (code == 99 || code == 118)) return true;
			//only number key
			if(code<48 || code>57)return false;
			return true;
		},
		//跳转框页面跳转
		gopage : function(){
			var str_page = $('#'+this._opts.gopageTextboxId).val();
			if(isNaN(str_page)){
				$('#'+this._opts.gopageTextboxId).val(this._opts.next);
				return;
			}
			var n = parseInt(str_page);
			if(n < 1) n = 1;
			if(n > this._opts.total) n = this._opts.total;
			if(this._opts.mode == 'click'){
				this._clickHandler(n);
			}else{
				window.location = this.getLink(n);
			}
		},
		/*//不刷新页面直接手动调用选中某一页码
		selectPage : function(n){
			this._opts['pno'] = n;
			this.generPageHtml(this._opts,true);
		},*/
		//生成控件代码
		generPageHtml : function(opts,enforceInit){
			/*if(enforceInit || !this.inited){
				this.init(opts);
			}*/
			var lang_text = this._opts.lang_text[this._opts.lang];
			if(this._opts.pno < 1) this._opts.pno = 1;
			this._opts.total = (this._opts.total <= 1) ? 1: this._opts.total;
			if(this._opts.pno > this._opts.total) this._opts.pno = this._opts.total;
			this._opts.prv = (this._opts.pno<=2) ? 1 : (this._opts.pno-1);
			this._opts.next = (this._opts.pno >= this._opts.total-1) ? this._opts.total : (this._opts.pno + 1);
			this._opts.hasPrv = (this._opts.pno > 1);
			this._opts.hasNext = (this._opts.pno < this._opts.total);
			var str_first='',str_prv='',str_next='',str_last='';
			if(this._opts.isShowFirstPageBtn){
				if(this._opts.hasPrv){
					str_first = '<a '+this._getHandlerStr(1)+' title="'
						+(lang_text.firstPageTipText || lang_text.firstPageText)+'">'+lang_text.firstPageText+'</a>';
				}else{
					str_first = '<span class="disabled">'+lang_text.firstPageText+'</span>';
				}
			}
			if(this._opts.isShowPrePageBtn){
				if(this._opts.hasPrv){
					str_prv = '<a '+this._getHandlerStr(this._opts.prv)+' title="'
						+(lang_text.prePageTipText || lang_text.prePageText)+'">'+lang_text.prePageText+'</a>';
				}else{
					str_prv = '<span class="disabled">'+lang_text.prePageText+'</span>';
				}
			}
			if(this._opts.isShowNextPageBtn){
				if(this._opts.hasNext){
					str_next = '<a '+this._getHandlerStr(this._opts.next)+' title="'
						+(lang_text.nextPageTipText || lang_text.nextPageText)+'">'+lang_text.nextPageText+'</a>';
				}else{
					str_next = '<span class="disabled">'+lang_text.nextPageText+'</span>';
				}
			}
			if(this._opts.isShowLastPageBtn){
				if(this._opts.hasNext){
					str_last = '<a '+this._getHandlerStr(this._opts.total)+' title="'
						+(lang_text.lastPageTipText || lang_text.lastPageText)+'">'+lang_text.lastPageText+'</a>';
				}else{
					str_last = '<span class="disabled">'+lang_text.lastPageText+'</span>';
				}
			}
			var str = '';
			var dot = '<span>...</span>';
			var total_info='';
			if(this._opts.isShowTotalPage || this._opts.isShowTotalRecords){
				total_info = '&nbsp;<span class="normalsize">'+lang_text.totalPageBeforeText;
				if(this._opts.isShowTotalPage){
					total_info += this._opts.total + lang_text.totalPageAfterText;
					if(this._opts.isShowTotalRecords){
						total_info += '/';
					}
				}
				if(this._opts.isShowTotalRecords){
					total_info += this._opts.totalRecords + lang_text.totalRecordsAfterText;
				}
				
				total_info += '</span>';
			}
			
			var gopage_info = '';
			if(this._opts.isGoPage){
				gopage_info = '&nbsp;'+lang_text.gopageBeforeText+'<span id="'+this._opts.gopageWrapId+'">'+
					'<input type="button" id="'+this._opts.gopageButtonId+'" value="'
						+lang_text.gopageButtonOkText+'" />'+
					'<input type="text" id="'+this._opts.gopageTextboxId+'" value="'+this._opts.next+'" /></span>'+lang_text.gopageAfterText;
			}
			
			var pnm_info = '';
			if(this._opts.isShowPnm){
				pnm_info = '&nbsp;' + '<select id="'+this._opts.showPnmSelectId+'"><option value="10">10</option><option value="15">15</option><option value="20">20</option><option value="30">30</option></select>';
			}
			
			//分页处理
			if(this._opts.total <= 8){
				for(var i=1;i<=this._opts.total;i++){
					if(this._opts.pno == i){
						str += '<span class="curr">'+i+'</span>';
					}else{
						str += '<a '+this._getHandlerStr(i)+' title="'
							+lang_text.buttonTipBeforeText + i + lang_text.buttonTipAfterText+'">'+i+'</a>';
					}
				}
			}else{
				if(this._opts.pno <= 5){
					for(var i=1;i<=7;i++){
						if(this._opts.pno == i){
							str += '<span class="curr">'+i+'</span>';
						}else{
							str += '<a '+this._getHandlerStr(i)+' title="'+
								lang_text.buttonTipBeforeText + i + lang_text.buttonTipAfterText+'">'+i+'</a>';
						}
					}
					str += dot;
				}else{
					str += '<a '+this._getHandlerStr(1)+' title="'
						+lang_text.buttonTipBeforeText + '1' + lang_text.buttonTipAfterText+'">1</a>';
					str += '<a '+this._getHandlerStr(2)+' title="'
						+lang_text.buttonTipBeforeText + '2' + lang_text.buttonTipAfterText +'">2</a>';
					str += dot;
					
					var begin = this._opts.pno - 2;
					var end = this._opts.pno + 2;
					if(end > this._opts.total){
						end = this._opts.total;
						begin = end - 4;
						if(this._opts.pno - begin < 2){
							begin = begin-1;
						}
					}else if(end + 1 == this._opts.total){
						end = this._opts.total;
					}
					for(var i=begin;i<=end;i++){
						if(this._opts.pno == i){
							str += '<span class="curr">'+i+'</span>';
						}else{
							str += '<a '+this._getHandlerStr(i)+' title="'
								+lang_text.buttonTipBeforeText + i + lang_text.buttonTipAfterText+'">'+i+'</a>';
						}
					}
					if(end != this._opts.total){
						str += dot;
					}
				}
			}
			
			str = str_first + str_prv + str + str_next + str_last + pnm_info + gopage_info + total_info;
			$("#"+this._opts.pagerid).parent().find(" .paginationForNum").remove().end().append("<div class='paginationForNum'>" + str + "</div>");
			$("#" + this._opts.gopageButtonId).click($.proxy(this.gopage, this));
			$("#" + this._opts.gopageTextboxId).focus($.proxy(this.focus_gopage, this)).keypress($.proxy(this.keypress_gopage, this)).blur($.proxy(this.blur_gopage, this));
			$("#" + this._opts.showPnmSelectId).on("change",$.proxy(this.changePnm, this)).find("option[value="+this._opts.pnm+"]").attr("selected", "selected");
			if(this._opts.mode == 'click' && !$("#"+this._opts.pagerid).parent().data("addClicked")){
				var that = this;
				$("#"+this._opts.pagerid).siblings(".paginationForNum").on("click", "a", function(){
					that._clickHandler(parseInt($(this).data("pagenum")));
					return false;
				}).data("addClicked", true);
			}
		},
		getData : function(n){
			var that = this, opts = that._opts, func = that._func;
			opts.pno = n || opts.pno;
			opts.pnm = $("#" + this._opts.showPnmSelectId).val() || opts.pnm;
			var aPageAttr =opts.key2key;
			for(var p in aPageAttr){
				var sAttr = aPageAttr[p];
				opts.requestData[sAttr] = opts[p] || opts[sAttr];
			}
			$.ajax({
				type: opts.type,
				url: opts.url,
				data: opts.requestData,
				beforeSend: function(){
					if (opts.beforeLoad != null) {
						return opts.beforeLoad();
					}
					return true;
				},
				success: function(data) {
					opts.analysisData(data);
					func && func(opts.responseData.list, opts.requestData);
					that.generPageHtml();
					opts.pno = opts.requestData.currentPage;			
					if (opts.afterLoad != null) {
						opts.afterLoad(data, true);
					}
				},
				error: function(e) {
					opts.responseData = null;
					func && func(null);
					
				},
				dataType: opts.dataType
			});
		},
		//分页按钮控件初始化
		init : function(ele, opts, func){
			this._opts = opts;
			this._ele = ele;
			this._func = func;
			this._opts.inited = true;
			this.getData(opts.requestData.currentPage || opts.pno);
		},
		_getHandlerStr : function(n){
			if(this._opts.mode == 'click'){
				return 'href="'+this.getHref(n) + '" data-pagenum="' + n + '"';
			}
			//link模式
			return 'href="'+this.getLink(n)+'"';
		},
		_clickHandler	: function(n){
			var res = false;
			if(this.click && typeof this.click == 'function'){
				res = this.click.call(this,n) || false;
			}
			return res;
		}
	};
	
	function _analysisData(data){
		if(data){
			var defaults = this, responseData = defaults.responseData = {}, requestData = defaults.requestData,
				aPageAttr =defaults.key2key;
			for(var p in data){
				if(p == "data"){
					responseData.list = data[p];
				}else if(p == "page"){
					responseData.page = data.page;
					for(var p1 in aPageAttr){
						var sAttr = aPageAttr[p1];
						defaults[p1] = responseData.page[sAttr];
					}
					requestData.currentPage = data.page.currentPage;
				}else
					responseData[p] = data[p];
			}
		}
	}
	$.fn.paginationForNum = function(options, func){
		var opts = $.extend(true, {}, $.fn.paginationForNum.defaults, options);
		return this.each(function(index, ele) {
			new PaginationForNum($(ele), opts, func);
		});
	};
	$.fn.paginationForNum.defaults = {
		url: "",	//数据请求地址
		type: "post",	//ajax请求类型
		dataType: "json",	//返回数据类型
		requestData: {},	//请求条件
		responseData: {},	//服务器响应 返回的数据
		analysisData: _analysisData,	//数据解析
		beforeLoad: $.noop,	//加载前调用
		afterLoad: $.noop,	//加载后调用
		pagerid 			: 'pager_info', //divID
		mode				: 'click', //模式(link 或者 click)
		pno					: 1, //当前页码
		pnm					: 15, //每页显示条数
		total				: 1, //总页码
		totalRecords		: 0, //总数据条数
		key2key				:{		//配置实际项目中试用的属性名称
			pno: "currentPage",
			pnm: "pageNum",
			total: "totalPage",
			totalRecords: "totalRecord"
		},
		isShowFirstPageBtn	: true, //是否显示首页按钮
		isShowLastPageBtn	: true, //是否显示尾页按钮
		isShowPrePageBtn	: true, //是否显示上一页按钮
		isShowNextPageBtn	: true, //是否显示下一页按钮
		isShowTotalPage 	: true, //是否显示总页数
		isShowTotalRecords 	: true, //是否显示总记录数
		isGoPage 			: true,	//是否显示页码跳转输入框
		isShowPnm			: true, //是否显示每页条数
		hrefFormer			: '', //链接前部
		hrefLatter			: '', //链接尾部
		gopageWrapId		: 'pager_gopage_wrap',
		gopageButtonId		: 'pager_btn_go',
		gopageTextboxId		: 'pager_btn_go_input',
		showPnmSelectId			: 'page_pnm_select',
		lang				: sys_language,
		lang_text				:{
			zh_cn:{
				firstPageText			: '首页',
				firstPageTipText		: '首页',
				lastPageText			: '尾页',
				lastPageTipText			: '尾页',
				prePageText				: '上一页',
				prePageTipText			: '上一页',
				nextPageText			: '下一页',
				nextPageTipText			: '下一页',
				totalPageBeforeText		: '共',
				totalPageAfterText		: '页',
				totalRecordsAfterText	: '条数据',
				gopageBeforeText		: '转到',
				gopageButtonOkText		: '确定',
				gopageAfterText			: '页',
				buttonTipBeforeText		: '第',
				buttonTipAfterText		: '页'
			},
			ja_jp:{
				firstPageText			: '最初',
				firstPageTipText		: '最初',
				lastPageText			: '最後',
				lastPageTipText			: '最後',
				prePageText				: '前',
				prePageTipText			: '前',
				nextPageText			: '次',
				nextPageTipText			: '次',
				totalPageBeforeText		: '全部',
				totalPageAfterText		: 'ページ',
				totalRecordsAfterText	: '件記録',
				gopageBeforeText		: '移動',
				gopageButtonOkText		: '確定',
				gopageAfterText			: 'ページ',
				buttonTipBeforeText		: '第',
				buttonTipAfterText		: 'ページ'
			},
			en: {
				firstPageText			: '首页',
				firstPageTipText		: '首页',
				lastPageText			: '尾页',
				lastPageTipText			: '尾页',
				prePageText				: '上一页',
				prePageTipText			: '上一页',
				nextPageText			: '下一页',
				nextPageTipText			: '下一页',
				totalPageBeforeText		: '共',
				totalPageAfterText		: '页',
				totalRecordsAfterText	: '条数据',
				gopageBeforeText		: '转到',
				gopageButtonOkText		: '确定',
				gopageAfterText			: '页',
				buttonTipBeforeText		: '第',
				buttonTipAfterText		: '页'
			}
		}
	};
	$.fn.paginationForNum.config = function(options){
		$.fn.paginationForNum.defaults = $.extend(true, {}, $.fn.paginationForNum.defaults, options);
	};
})(jQuery);